package com.jd.xubobo.aoplearn.web;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Description 切面
 * @Author xubobo1
 * @Date 2018/9/18
 */
@Aspect
@Component
@Order(5)
public class DemoAspect {
    private static final Logger logger = LoggerFactory.getLogger(DemoAspect.class);

    @Pointcut("execution(public * com.jd.xubobo.aoplearn.web..*.*(..))")
    public void pointcut(){}

    @Around("pointcut()")
    public Object doSometingBefore(ProceedingJoinPoint joinPoint){
        Object object;
        try {
            logger.info("切面开始执行");
            logger.info("CLASS_METHOD:{}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
            logger.info("ARGS:{}", Arrays.toString(joinPoint.getArgs()));
            object = joinPoint.proceed();
            logger.info("执行结束，执行返回的结果为{}", object);
        } catch (Throwable e){
            object = null;
            logger.error("执行方法{}时发生异常", joinPoint.getSignature().getName(), e);
        }
        return object;
    }

}
